package com.simin.topicdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simin.topicdemo.utils.ContentTextUtil;
import com.simin.topicdemo.view.FlowLayout;
import com.simin.topicdemo.view.TopicEditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TopicEditText mEdittext;
    private FlowLayout topicView;
    private List<String> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textview = findViewById(R.id.textView);
        textview.setText(ContentTextUtil.getWeiBoContent("#河南春晚新年虎来个# 新的一年，期待和每一个坚定前行的你，惊喜相遇！1月30日19:30  ， @河南春晚官方 我和唐小妹一起，陪你欢欢喜喜过大年！#河南春晚全阵容官宣", this, textview));
        mEdittext = findViewById(R.id.ed_text);
        topicView = findViewById(R.id.topic_view);
        mEdittext.tag().tagColor(Color.BLUE);      //设置话题颜色
        inittopic();
    }

    /**
     * 话题
     */
    private void inittopic() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5, 2, 0, 0);
        if (topicView != null) {
            topicView.removeAllViews();
        }
        mList.add("#添加话题");
        mList.add("#美食好吃街");
        mList.add("#重庆网红打卡");
        mList.add("#长沙小吃");
        mList.add("#西安大唐不夜城");
        mList.add("#附近哪里好玩");
        mList.add("#最近的热点是什么呢");
        mList.add("#看看大家在干什么");
        for (int i = 0; i < mList.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout_topic, null);
            final TextView tv = view.findViewById(R.id.txt_topic);
            tv.setText(mList.get(i));
            tv.setSingleLine();
            tv.setLayoutParams(layoutParams);
            topicView.addView(view, layoutParams);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (tv.getText().toString().equals("#添加话题")) {
                        EditTextDialog mRemarkDialog = new EditTextDialog(MainActivity.this);
                        mRemarkDialog.setCancelable(true);
                        mRemarkDialog.setCanceledOnTouchOutside(true);
                        mRemarkDialog.show();
                        return;
                    } else {
                        int index = mEdittext.getSelectionStart();
                        Editable editable = mEdittext.getText();
                        editable.insert(index, tv.getText().toString().trim() + " ");
                    }
                }
            });
        }
    }

    public class EditTextDialog extends Dialog {

        public EditTextDialog(@NonNull Activity context) {
            super(context, R.style.MyDialog);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_pop);
            final EditText edcontent = findViewById(R.id.ed_content);
            findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
            findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String content = edcontent.getText().toString().trim();
                    if (!TextUtils.isEmpty(content)) {
                        int index = mEdittext.getSelectionStart();
                        Editable editable = mEdittext.getText();
                        editable.insert(index, "#" + content + " ");
                        mList.add(1, content);
                        inittopic();
                        dismiss();
                    }
                }
            });
        }
    }

}
